<?php

Yii::import('application.models._base.BaseAuctionProductRelations');


class AuctionProductRelations extends BaseAuctionProductRelations
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	protected $_product_title;
	
	public function rules(){
	    $rules = array_merge(parent::rules(),array(
	        array('product_title','safe' )
	    ));
	
	    return $rules;
	}
	
	public function findByPk($pk, $condition = '', $params = Array()){
	    $this->auction_id = $pk;
	    return $this->findByAttributes(array('auction_id' => $pk));
	}
	
	public function setProduct_title($productTitle){
	    $product = ProductInfo::model()->findByAttributes(array('title' => $productTitle));
	    if (!empty($product)){
	        $this->product_id = $product->id;
	    }
	    return $this->_product_title = $productTitle;
	}
	
	public function getProduct_title(){
	    if (!empty($this->_product_title)){
	        return $this->_product_title;
	    }
	    if (!empty($this->auction_id)){
	        $productRelations = $this->with('product')->findByAttributes(array('auction_id' => $this->auction_id));
	        return CHtml::value($productRelations, 'product.title');
	    }
	    return null;
	}
	
	public function relations(){
	    return array(
	        'product' =>  array(self::BELONGS_TO,'ProductInfo','','on' => 't.product_id = product.id'  ),
	    );
	}
}